import { Component, OnInit, Input, EventEmitter } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import * as _ from 'lodash';
import { DataService } from "../../services/data.service";

@Component({
	selector: 'app-breadcrumb',
	templateUrl: './breadcrumb.component.html',
	styleUrls: ['./breadcrumb.component.scss']
})
export class AppBreadcrumbComponent implements OnInit {

	@Input() public selectedTab: Array<any> = [];

	list = [];
	@Input() public currentUrl: String = "";

	currentItem = {
		title: ""
	};

	navTab(item) {
		this.router.navigate([item.url]);
		sessionStorage.setItem('currentTitle', item.title);
	}

	closeTab(item) {
		if (this.selectedTab.length > 1) {
			_.remove(this.selectedTab, item);
			let last = this.selectedTab[this.selectedTab.length - 1];
			this.router.navigate([last.url]);

		}
	}

	constructor(
		public router: Router,
		public dataService: DataService
	) {
		// console.log("---AppBreadcrumbComponent----");
		// console.log(this.selectedTab);
		this.router.events.subscribe((evt: NavigationEnd) => {
			this.list = JSON.parse(sessionStorage.getItem('breadcrumbList'));
			this.currentItem = _.last(this.list);
		});
	}

	ngOnInit() {
		let that = this;
		// 新增成功后删除tab
		this.dataService.public_url = new EventEmitter();
		this.dataService.public_url.subscribe((value: any) => {
			if (value) {
				console.log("--url--" + JSON.stringify(value));
				let item = _.find(that.selectedTab, value);
				that.closeTab(item);
			}
		})
	}

	//点击进度条滚动
	scrollbars(item) {
		if (item == 1) {
			window['$']('.scrolltab').scrollLeft(0);
		} else if (item == 2) {
			window['$']('.scrolltab').scrollLeft(window['$']('.scrolltab')[0].scrollWidth);
		}
	}
}
